package com.qf.hospital.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.hospital.DTO.ReportDTO;
import com.qf.hospital.VO.ReportVO;
import com.qf.hospital.entity.Report;
import com.qf.hospital.entity.ReportExample;
import com.qf.hospital.enums.HospitalExceptionEnum;
import com.qf.hospital.exception.HosptialException;
import com.qf.hospital.mapper.ReportMapper;
import com.qf.hospital.service.ReportService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 何星宇
 * @version V1.0
 * @Date 2021/10/19 21:48
 */

@Service
public class ReportServiceImpl implements ReportService {
    @Autowired
    private ReportMapper mapper;
    private final Integer DELETE = 0;
    private Logger log = LoggerFactory.getLogger(ReportServiceImpl.class);

    @Override
    public Integer findReportTotal() {
        ReportExample example = new ReportExample();
        example.createCriteria().andStateNotEqualTo(DELETE);
        long count = mapper.countByExample(example);
        return Integer.valueOf(count+"");
    }

    @Override
    public Integer findReportCurrent() {
        long count = mapper.findReportByCurrent();
        return Integer.valueOf(count+"");
    }

    /**
     * 根据患者信息查询挂号信息
     * @param cc
     * @param name
     * @return
     */
    @Override
    public List<ReportVO> findPatientInfo(Integer cc, String name) {
        List<Report> reportList = mapper.findPatientInfo(cc,name);
        //封装数据
        List<ReportVO> data = new ArrayList<>();
        for (Report report : reportList) {
            ReportVO reportVO = new ReportVO();
            BeanUtils.copyProperties(report, reportVO);
            data.add(reportVO);
        }
        return data;
    }

    /**
     * 手机号&身份证号校验
     * @param phone
     * @param carid
     * @return
     */
    @Override
    public Integer checkPhoneAndCarid(String phone, String carid) {
        Integer count = mapper.countByValue("phone",phone);
        if (count>0){
            return 1;
        }
        count = mapper.countByValue("carid",carid);
        return count>0?2:3;
    }

    /**
     * 添加患者信息
     * @param reportDTO
     */
    @Override
    @Transactional
    public void addReport(ReportDTO reportDTO) {
        //封装数据
        Report report = new Report();
        BeanUtils.copyProperties(reportDTO, report);
        //添加
        int count = mapper.insertSelective(report);
        if (count!=1){
            //记录日志
            log.error("【系统异常】 添加患者信息失败");
            //抛出异常
            throw new HosptialException(HospitalExceptionEnum.SYSTEM_EXCEPTION,"添加患者信息失败");
        }
    }

    /**
     * 删除患者信息
     * @param id report表主键id
     */
    @Override
    @Transactional
    public void deleteReportById(Integer id) {
        //封装数据
        Report report = new Report();
        report.setReportId(id);
        report.setState(0);
        //修改数据
        int count = mapper.updateByPrimaryKeySelective(report);
        if (count!=1){
            //记录日志
            log.error("【系统异常】 删除患者信息失败");
            //抛出异常
            throw new HosptialException(HospitalExceptionEnum.SYSTEM_EXCEPTION,"删除患者信息失败");
        }
    }

    /**
     * 查询当天患者信息
     * @param name
     * @param page
     * @param limit
     * @return
     */
    @Override
    public PageInfo<Report> findTheDayPersonByPage(String name, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        List<Report> reportList = mapper.findTheDayPerson(name);
        PageInfo<Report> pageInfo = new PageInfo<>(reportList);
        return pageInfo;
    }

    /**
     * 查询患者病因
     * @param reportId 患者id
     * @return
     */
    @Override
    public String findPathogenyByReportId(Integer reportId) {
        ReportExample example = new ReportExample();
        example.createCriteria().andReportIdEqualTo(reportId);
        List<Report> reportList = mapper.selectByExample(example);
        if (reportList.size()>1){
            //记录日志
            log.error("【系统异常】 相同患者存在多个");
            throw new HosptialException(HospitalExceptionEnum.SYSTEM_EXCEPTION,"相同患者存在多个");
        }
        if (reportList==null){
            return null;
        }
        return reportList.get(0).getZhuan();
    }

    /**
     * 转住院
     * @param report
     */
    @Override
    @Transactional
    public void updateReport(Report report) {
        int count = mapper.updateByPrimaryKeySelective(report);
        if (count!=1){
            //记录日志
            log.error("【系统异常】 转住院失败");
            //抛出异常
            throw new HosptialException(HospitalExceptionEnum.SYSTEM_EXCEPTION,"转住院失败");
        }
    }
}
